package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TDoctor;
import com.cookor.poi.modules.phy.service.TDoctorService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.cookor.poi.modules.tenant.entity.TTenant;
import com.cookor.poi.modules.tenant.service.TTenantService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tDoctor"})
public class TDoctorController extends BaseController {
  @Autowired
  private TDoctorService tDoctorService;
  
  @Autowired
  private TTenantService tTenantService;
  
  @ModelAttribute
  public TDoctor get(@RequestParam(required = false) String id) {
    TDoctor entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tDoctorService.get(id); 
    if (entity == null)
      entity = new TDoctor(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tDoctor:view"})
  @RequestMapping({"list", ""})
  public String list(TDoctor tDoctor, HttpServletRequest request, HttpServletResponse response, Model model) {
    tDoctor.setTenantId(UserUtils.getTenantId());
    Page<TDoctor> page = this.tDoctorService.findPage(new Page(request, response), tDoctor);
    model.addAttribute("page", page);
    model.addAttribute("tDoctor", tDoctor);
    return "modules/phy/tDoctorList";
  }
  
  @RequiresPermissions({"phy:tDoctor:view"})
  @RequestMapping({"form"})
  public String form(TDoctor tDoctor, Model model) {
    if (tDoctor.getOnlineStatus() == null)
      tDoctor.setOnlineStatus(Integer.valueOf(0)); 
    if (tDoctor.getBookStatus() == null)
      tDoctor.setBookStatus(Integer.valueOf(1)); 
    if (tDoctor.getDimission() == null)
      tDoctor.setDimission(Integer.valueOf(0)); 
    model.addAttribute("tDoctor", tDoctor);
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tenantList", this.tTenantService.findList(new TTenant()));
    return "modules/phy/tDoctorForm";
  }
  
  @RequiresPermissions({"phy:tDoctor:edit"})
  @RequestMapping({"save"})
  public String save(TDoctor tDoctor, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tDoctor, new Class[0]))
      return form(tDoctor, model); 
    this.tDoctorService.save(tDoctor);
    addMessage(redirectAttributes, new String[] { "保存医生成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tDoctor/?repage";
  }
  
  @RequiresPermissions({"phy:tDoctor:edit"})
  @RequestMapping({"delete"})
  public String delete(TDoctor tDoctor, RedirectAttributes redirectAttributes) {
    this.tDoctorService.delete(tDoctor);
    addMessage(redirectAttributes, new String[] { "删除医生成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tDoctor/?repage";
  }
  
  @RequiresPermissions({"user"})
  @ResponseBody
  @RequestMapping({"treeData"})
  public List<Map<String, Object>> treeData(@RequestParam(required = false) String doctorName, HttpServletResponse response) {
    List<Map<String, Object>> mapList = Lists.newArrayList();
    TDoctor tDoctor = new TDoctor();
    tDoctor.setDoctorName(doctorName);
    tDoctor.setTenantId(UserUtils.getTenantId());
    List<TDoctor> list = this.tDoctorService.findList(tDoctor);
    for (int i = 0; i < list.size(); i++) {
      String s = "";
      TDoctor e = list.get(i);
      Map<String, Object> map = Maps.newHashMap();
      map.put("id", e.getId());
      if (UserUtils.getTenantId() == null)
        s = s + "【" + e.getTenant().getTenantName() + "】"; 
      if (StringUtils.isNotBlank(e.getRemarks())) {
        map.put("name", s + e.getDoctorName() + "(" + e.getRemarks() + ")");
      } else {
        map.put("name", s + e.getDoctorName());
      } 
      mapList.add(map);
    } 
    return mapList;
  }
}
